package com.project.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.project.bean.UserBean;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("account")
public class UserController {
    @Autowired
    private IUserService service;

    @RequestMapping("findAll")
    public PageInfo<UserBean> findAll(Integer pageNO){
        return service.findAll(pageNO);
    }

    @RequestMapping("add")
    public String add(@Valid UserBean user, BindingResult result) throws Exception {
        if(result.hasErrors()){
            return new ObjectMapper().writeValueAsString(result.getFieldErrors());
        }
        service.add(user);
        return "ok";
    }

}

